<template>
  <div>
    <a-card :bordered="false" class="ant-pro-components-tag-select" v-show="!hiddenSearch">
      <div class="table-page-search-wrapper">
        <a-form layout="inline">
          <a-row :gutter="28">
            <a-col :md="5" :sm="12">
              <a-form-item label="车牌号">
                <a-input v-model="queryParam.plateNo" placeholder="请输入车牌号"/>
              </a-form-item>
            </a-col>
            <a-col :md="5" :sm="12">
              <a-form-item label="号牌颜色">
                <a-select placeholder="请选择号牌颜色" v-model="queryParam.plateColor" @change="handlePlateColorChange">
                  <a-select-option v-for="(action) in dictDataPlateColor" :key="action.dictValue">
                    {{ action.dictLabel }}
                  </a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
            <a-col :md="5" :sm="12">
              <a-form-item label="设备SN">
                <a-input v-model="queryParam.deviceNo" placeholder="请输入设备SN"/>
              </a-form-item>
            </a-col>
            <a-col :md="5" :sm="12">
              <a-form-item label="缓存状态">
                <a-select placeholder="请选择缓存状态" v-model="queryParam.status" @change="handleCacheStatusChange">
                  <a-select-option v-for="(action) in dictDataCacheStatus" :key="action.dictValue">
                    {{ action.dictLabel }}
                  </a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
            <a-col :md="1" :sm="24">
              <span class="table-page-search-submitButtons">
                <a-tooltip placement="topLeft" title="查询" arrow-point-at-center>
                  <a-button shape="circle" icon="search" @click="$refs.table.refresh(true)"/>
                </a-tooltip>
                <a-tooltip placement="topLeft" title="重置" arrow-point-at-center>
                  <a-button style="margin-left: 8px" @click="() => this.queryParam = {}" icon="sync"/>
                </a-tooltip>
              </span>
            </a-col>
          </a-row>
        </a-form>
      </div>
    </a-card>
    <a-card
      style="margin-top: 10px"
      :bordered="false">
      <div class="table-operator">
        <a-tooltip placement="topLeft" title="隐藏/显示搜索栏" arrow-point-at-center>
          <a-button size="small" style="position: absolute;right: 55px;top: 6px;" shape="circle" :icon="icon" @click="hiddenSearchMenu(hiddenSearch)"/>
        </a-tooltip>
      </div>
      <s-table
        :scroll="{ x: 1500 }"
        ref="table"
        row-key="id"
        size="middle"
        :columns="columns"
        :data="loadData"
        :rowSelection="removeEnable ? {selectedRowKeys: selectedRowKeys, onChange: onSelectChange} : null"
        showPagination="auto"
        :showTotal="true">
        <span slot="videoTime" slot-scope="videoTime">
          {{ conversionVideoTime (videoTime) }}
        </span>
        <span slot="plateColor" slot-scope="text">
          <a-badge :color="text | plateColorFilter" :text="text | plateColorTextFilter"/>
        </span>
        <span slot="status" slot-scope="text">
          <a-badge :status="text | statusFilter" :text="text | statusTextFilter"/>
        </span>
      </s-table>
    </a-card>
  </div>
</template>

<script>
import { checkPermission } from '@/utils/permissions'
import moment from 'moment'
import {
  batchDelPlateRule,
  dictDataList, findOriginalDataPage
} from '@/api/system'
import { STable } from '@/components'
import message from 'ant-design-vue/es/message'
const plateColorMap = {
  '1': {
    color: 'blue',
    text: '蓝'
  },
  '2': {
    color: 'yellow',
    text: '黄'
  },
  '3': {
    color: 'black',
    text: '黑'
  },
  '4': {
    color: '#f5f2f2',
    text: '白'
  },
  '5': {
    color: 'green',
    text: '绿'
  },
  '9': {
    color: 'red',
    text: '其他'
  }
}
const statusMap = {
  '0': {
    status: 'default',
    text: '未缓存'
  },
  '1': {
    status: 'error',
    text: '缓存失败'
  },
  '2': {
    status: 'success',
    text: '缓存成功'
  }
}
export default {
  components: {
    STable
  },
  name: 'Source',
  data () {
    return {
      // 查询参数
      queryParam: {},
      icon: 'eye-invisible',
      hiddenSearch: false,
      removeEnable: checkPermission('setting:plate:remove'),
      addRuleEnable: checkPermission('setting:plate:add'),
      editRuleEnable: checkPermission('setting:plate:edit'),
      selectedRowKeys: [],
      selectedRows: [],
      dictDataCacheStatus: [],
      dictDataPlateColor: [],
      // 表头
      columns: [
        {
          title: '车牌号',
          dataIndex: 'plateNo',
          align: 'center',
          width: 100,
          fixed: 'left'
        },
        {
          title: '车牌颜色',
          dataIndex: 'plateColor',
          align: 'center',
          width: 80,
          scopedSlots: { customRender: 'plateColor' }
        },
        {
          title: '设备SN',
          dataIndex: 'deviceNo',
          align: 'center',
          width: 100
        },
        {
          title: '设备排列号',
          dataIndex: 'channelNo',
          align: 'center',
          width: 100
        },
        {
          title: '违停时间',
          dataIndex: 'illegalTime',
          align: 'center',
          width: 150,
          ellipsis: true,
          customRender: (text, row, index) => {
            return moment(Number(text)).format('YYYY-MM-DD HH:mm:ss')
          }
        },
        {
          title: '截取视频时间',
          dataIndex: 'videoTime',
          align: 'center',
          width: 300,
          ellipsis: true,
          scopedSlots: { customRender: 'videoTime' }
        },
        {
          title: '入库时间',
          dataIndex: 'createTime',
          align: 'center',
          width: 150,
          ellipsis: true,
          sorter: true
        },
        {
          title: '缓存状态',
          dataIndex: 'status',
          align: 'center',
          width: 150,
          ellipsis: true,
          scopedSlots: { customRender: 'status' }
        }
      ],
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        return findOriginalDataPage(Object.assign(parameter, this.queryParam))
          .then(res => {
            return res.data
          })
      }
    }
  },
  created () {
    this.loadDictDataCacheStatus()
    this.loadDictDataPlateColor()
  },
  filters: {
    plateColorTextFilter (type) {
      return plateColorMap[type].text
    },
    plateColorFilter (type) {
      return plateColorMap[type].color
    },
    statusTextFilter (type) {
      return statusMap[type].text
    },
    statusFilter (type) {
      return statusMap[type].status
    }
  },
  methods: {
    conversionVideoTime (videoTime) {
      var startTimeStr = ''
      var endTimeStr = ''
      var times = videoTime.split(',')
      for (var i = 0; i < times.length; i++) {
        if (i !== times.length - 1) {
          startTimeStr = '[' + moment(Number(times[i])).format('YYYY-MM-DD HH:mm:ss') + ' - '
        } else {
          endTimeStr = moment(Number(times[i])).format('YYYY-MM-DD HH:mm:ss') + ']'
        }
      }
      return startTimeStr + endTimeStr
    },
    handleEdit (record) {
      this.$refs.modal.edit(record)
    },
    handleOk () {
      this.$refs.table.refresh()
    },
    handleCacheStatusChange (value) {
      this.queryParam.status = value
    },
    handlePlateColorChange (value) {
      this.queryParam.plateColor = value
    },
    loadDictDataCacheStatus () {
      dictDataList({ dictType: 'cache_status' }).then(res => {
        this.dictDataCacheStatus = res.data
      })
    },
    loadDictDataPlateColor () {
      dictDataList({ dictType: 'plate_color' }).then(res => {
        this.dictDataPlateColor = res.data
      })
    },
    onSelectChange (selectedRowKeys, selectedRows) {
      this.selectedRowKeys = selectedRowKeys
      this.selectedRows = selectedRows
    },
    hiddenSearchMenu (hiddenSearch) {
      this.hiddenSearch = hiddenSearch !== true
      this.icon = this.hiddenSearch === true ? 'eye' : 'eye-invisible'
    },
    onClick (ids) {
      const self = this
      this.$confirm({
        title: '你确定删除吗?',
        content: h => <div style="color:red;">删除将永久不能恢复</div>,
        onOk () {
          batchDelPlateRule(ids).then(res => {
            self.$refs.table.refresh()
            message.success('操作成功')
            self.selectedRowKeys = []
          })
        },
        onCancel () {
          self.selectedRowKeys = []
        }
      })
    }
  }
}
</script>

<style lang="less" scoped>
  /deep/.ant-form-item-control {
    position: relative;
    line-height: 30px;
    zoom: 1;
  }
</style>
